package c0907m.group2.aitsm.util;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;

/**
 * User: Hoang Duy
 * Date: 5/2/11
 * Time: 1:26 AM
 */
public class DateSynchronizer {
    private JComboBox cboDay;
    private JComboBox cboMonth;
    private JComboBox cboYear;
    public static final int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public static final String[] months = {"January", "February", "March",
            "April", "May", "June", "July", "August", "September", "October",
            "November", "December"};
    private int currentYear;
    private int upBound;
    private int lowBound;

    /**
     * Create new DateSynchronizer for a combo of 3 JComboBox day,month,year
     * with a specified range of years
     *
     * @param _day     JComboBox represents day
     * @param _month   JComboBox represents month
     * @param _year    JComboBox represents year
     * @param upBound  up-bound range
     * @param lowBound low-bound range
     */
    public DateSynchronizer(JComboBox _day, JComboBox _month, JComboBox _year, int upBound, int lowBound) {
        this.cboDay = _day;
        this.cboMonth = _month;
        this.cboYear = _year;
        this.currentYear = Calendar.getInstance().get(Calendar.YEAR);
        this.lowBound = currentYear - lowBound;
        this.upBound = currentYear + upBound;
        init();
    }

    private void init() {
        for (int i = upBound; i >= lowBound; i--) {
            cboYear.addItem(i);
        }
        cboYear.setSelectedItem(currentYear);
        cboYear.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                synchronizeDate();
            }
        });

        for (String m : months)
            cboMonth.addItem(m);
        cboMonth.setSelectedIndex(0);
        cboMonth.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                synchronizeDate();
            }
        });

        for (int i = 1; i <= 31; i++) {
            cboDay.addItem(i);
        }
        cboDay.setSelectedIndex(0);
    }

    /**
     * Handler for combo-box when iteStateChanged event fires
     */
    private void synchronizeDate() {
        int currentSelectedDay = (Integer) cboDay.getSelectedItem();
        int fromMonth = cboMonth.getSelectedIndex();

        cboDay.removeAllItems();
        for (int i = 1; i <= days[fromMonth]; i++) {
            cboDay.addItem(i);
        }

        if ((Integer) cboYear.getSelectedItem() % 4 == 0
                && fromMonth == 1) {
            cboDay.addItem(29);
        }

        if (cboDay.getItemCount() < currentSelectedDay)
            cboDay.setSelectedItem(cboDay.getItemCount());
        else
            cboDay.setSelectedItem(currentSelectedDay);
    }

}
